<?php

namespace app\controllers;

use Yii;
use app\models\GkyWeAccount;
use app\models\GkyWeAccountSearch;
use app\models\GkyWeMenu;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * GkyWeAccountController implements the CRUD actions for GkyWeAccount model.
 */
class GkyWeAccountController extends Controller
{
     public $layout='main_1';
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all GkyWeAccount models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new GkyWeAccountSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single GkyWeAccount model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new GkyWeAccount model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new GkyWeAccount();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing GkyWeAccount model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $weAccountSearchModel = new GkyWeAccountSearch();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
                'weAccountSearchModel'=>$weAccountSearchModel,
            ]);
        }
    }

    /**
     * Deletes an existing GkyWeAccount model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }
    
    //子菜单数据获取
    public function actionMenu(){
        $accountid= \Yii::$app->request->post('accountid');
        $gkywemenu= GkyWeMenu::find()->asArray()->where(['account_id'=>$accountid])->all();
        echo json_encode(array('gkywemenu'=>$gkywemenu));
    }
    
    //删除二级子菜单
    public function actionDelsecond(){
        $secondid=  \Yii::$app->request->post('secondid');
        $str=  \Yii::$app->request->post('str');
        $menu= GkyWeMenu::findOne(['id'=>$secondid]);
        $res="";
        if($menu->delete()){                
            $res=1;
        }else{
            $res=0;
        }
        //如果删除的是一级菜单，还要把它的子菜单删除
        if($str=="fir"){
            if(GkyWeMenu::find()->where(['pid'=>$secondid])->all()){
                if(GkyWeMenu::deleteAll(['pid'=>$secondid])){
                    $res=1;
                }else{
                    $res=0;
                } 
            }
        }
        echo json_encode(array('res'=>$res));
    }
    
    //检测是否含有3个一级菜单
    public function actionCheckfir(){
        $accountid=  \Yii::$app->request->post('accountid');
        $sql="select pid from gky_we_menu where pid=0 and account_id='$accountid'";
        $result=  GkyWeMenu::findBySql($sql)->all();
        $res="";
        if(count($result)<3){
            $res=1;
        }else{
            $res=0;
        }
        echo json_encode(array('res'=>$res));
    }
    
    //修改一级二级菜单的名称
    public function actionUpdatename(){
        $fandsid=  \Yii::$app->request->post('fandsid');
        $name=  \Yii::$app->request->post('name');
        $accountid=  \Yii::$app->request->post('accountid');
        //如果fandsid为insert，则为插入子菜单
        $result="";
        if(substr($fandsid, 0,6)=='insert'){
            $menu=new GkyWeMenu();
            $menu->name=$name;
            $menu->account_id=$accountid;
            $menu->pid= substr($fandsid, 6);
            $menu->isSet=0;
            $sql="select sort from gky_we_menu where pid='$menu->pid' order by sort desc limit 1";
            $sort=GkyWeMenu::findBySql($sql)->all();
            if(!$sort){
                $menu->sort=0;
            }else{
                $menu->sort=$sort[0]['sort']+1;
            }
            $menu->type=9;
            $result=$menu->save();
        }
        if($fandsid=="insertfir"){//插入一级菜单
            $menu=new GkyWeMenu();
            $menu->account_id=$accountid;
            $menu->pid=0;
            $menu->name=$name;
            $menu->isSet=0;
            $sql="select sort from gky_we_menu where pid=0 order by sort desc limit 1";
            $sort=GkyWeMenu::findBySql($sql)->all();
            if(!$sort){
                $menu->sort=0;
            }else{
                $menu->sort=$sort[0]['sort']+1;
            }
            $menu->type=9;
            $result=$menu->save();
        }
        if((substr($fandsid, 0,6)!='insert')&&($fandsid!="insertfir")){//直接更新
             $menu=  GkyWeMenu::findOne(['id'=>$fandsid]);
             $menu->name=$name;
             $result=$menu->save();
        }
        $res="";
        if($result){
            $res=1;
        }else{
            $res=0;
        }
        echo json_encode(array('res'=>$res));
    }
    
     //更新动作按钮
    public function actionUpdateactive(){
        $active=Yii::$app->request->post('active');
        $menuid=  Yii::$app->request->post('menuid');
        $txtkey=  Yii::$app->request->post('txtkey');
        $txturl=  Yii::$app->request->post('txturl');
        $menu=  GkyWeMenu::findOne(['id'=>$menuid]);
        if($active=='key'){
            $menu->key=$txtkey;
            $menu->type=1;
            $menu->url="";
        }else if($active=='url'){
            $menu->url=$txturl;
            $menu->type=2;
            $menu->key="";
        }
        $res="";
        if($menu->update()){
            $res=1;
        }else{
            $res=0;
        }
        echo json_encode(array('res'=>$res));
    }
    //获取动作按钮数据
    public function actionGetactive(){
        $menuid=Yii::$app->request->post('menuid');
        $menu=GkyWeMenu::findOne(['id'=>$menuid]);
        echo json_encode(array('url'=>$menu->url,'key'=>$menu->key));
    }

    /**
     * 切换当前公众号
     */
    public function actionChangeCurrent($id){

        $model = $this->findModel($id);

        $session = Yii::$app->session;
        $session->set('weixin',$model);

        $searchModel = new GkyWeAccountSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * 查看二维码
     */
    public function actionShowQrcode(){

        return $this->render('qrcode');
    }

    /**
     * 获得二维码
     */
    public function actionQrcode(){

        if(Yii::$app->request->isAjax){
            $id = Yii::$app->request->post('id');
            $qrcode = GkyWeAccount::findQrcodeById($id);
            return json_encode([
                'code' => 1,
                'qrcode' => $qrcode
            ]);
        }else{
            return json_encode([
                'code' => 0,
            ]);
        }
    }

    /**
     * Finds the GkyWeAccount model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return GkyWeAccount the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = GkyWeAccount::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
